from matplotlib.pyplot import *
from collections import defaultdict
import random


# function to calculate distance
def dist(p1, p2):
    return ((p1[0]-p2[0])**2)**0.5
# print (dist(1,3))

    # randomly generate around 100 cartesian coordinates

# A=[0.7585763931274414, 0.8314967155456543, 0.8880138397216797, 0.8885025978088379, 0.9186863899230957,
#      1.6231417655944824, 1.6403794288635254, 1.6445517539978027, 1.80739164352417, 1.8272995948791504,
#      1.8358826637268066, 1.9417881965637207]
A=[0.6981611251831055, 0.7831692695617676, 0.8962631225585938, 1.018381118774414, 1.0944604873657227, 1.244509220123291, 1.4136552810668945, 1.6789436340332031, 1.693558692932129, 1.7237067222595215, 1.7345428466796875, 1.9257068634033203]
all_points=[]
for i in range(len(A)):
    all_points.append([A[i]])
# print all_points
        # take radius = 8 and min. points = 8
E = 0.151212
minPts = 2

# find out the core points
other_points = []
core_points = []
plotted_points = []
for point in all_points:
    point.append(0)  # assign initial level 0
    total = 0
    # print point
    for otherPoint in all_points:
        # print(other_points)
        distance = dist(otherPoint, point)
        if distance <= E:
            total += 1

    if total > minPts:
        core_points.append(point)
        plotted_points.append(point)
    else:
        other_points.append(point)
        # find border points
border_points = []
for core in core_points:
    for other in other_points:
        if dist(core, other) <= E:
            border_points.append(other)
            plotted_points.append(other)

            # implement the algorithm
cluster_label = 0

for point in core_points:
    # print point
    if point[1] == 0:
        cluster_label += 1
        point[1] = cluster_label
        print cluster_label
    for point2 in plotted_points:
        distance = dist(point2, point)
        if point2[1] == 0 and distance <= E:
            # print point, point2
            point2[1] = point[1]

            # after the points are asssigned correnponding labels, we group them
cluster_list = defaultdict(lambda: [[], []])
for point in plotted_points:
    cluster_list[point[1]][0].append(point[0])
    cluster_list[point[1]][1].append(point[1])

# markers = ['+', '*', '.', 'd', '^', 'v', '>', '<', 'p']

# plotting the clusters
# i = 0
print cluster_list
# for value in cluster_list:
#     cluster = cluster_list[value]
#     # plot(cluster[0], cluster[1], markers[i])
#     i = i % 10 + 1

    # plot the noise points as well
# noise_points = []
# for point in all_points:
#     if not point in core_points and not point in border_points:
#         noise_points.append(point)
# noisex = []
# noisey = []
# for point in noise_points:
#     noisex.append(point[0])
#     noisey.append(point[1])
# print noisex
# plot(noisex, noisey, "x")

# title(str(len(cluster_list)) + " clusters created with E =" + str(E) + " Min Points=" + str(
#     minPts) + " total points=" + str(len(all_points)) + " noise Points = " + str(len(noise_points)))
# axis((0, 60, 0, 60))
# show()